package com.cxc.zk.distributed.demo;


import java.text.SimpleDateFormat;
import java.util.Date;

public class Test  implements  Runnable{

    private OrderNumGenerator orderNumGenerator = new OrderNumGenerator();

    private  Lock  lock = new ZkDistrbuteLock();

    public void run() {
        getOrderNumber();
    }

    public void getOrderNumber(){
        try {
            lock.getLock();
            String number = orderNumGenerator.getNumber();
            System.out.println("线程:" + Thread.currentThread().getName() + ",生成订单id:" + number);
        } catch (Exception e) {

        } finally {
            lock.unLock();
        }
    }

    public static void main(String[] args) {
        System.out.println("多线程生成number");
        for (int i = 0; i < 100; i++) {
            new Thread(new Test()).start();
        }
    }

}

class  OrderNumGenerator{
    // 生成订单号规则
    private static int count = 0;

    public String getNumber() {
        try {
            Thread.sleep(200);
        } catch (Exception e) {
        }
        SimpleDateFormat simpt = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        return simpt.format(new Date()) + "-" + ++count;
    }
}
